//
//  WYSendOutController.m
//  Boss
//
//  Created by admin on 2018/6/7.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYSendOutController.h"
#import "WYRedSectionView.h"
#import "WYRedPacketListCell.h"
#import "WYSendRedPacketList.h"
#import "WYMYReceive.h"
#import "WYSelectYearView.h"
#import "WYNearRedPacket.h"
#import "WYRedPacketMessageController.h"
@interface WYSendOutController ()<UITableViewDelegate,UITableViewDataSource,WYRedSectionViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)WYMYReceive *receive;

@property (nonatomic,strong)WYRedSectionView *sectionView;
@property (nonatomic,assign)BOOL refresh;
@property (nonatomic,strong) NSString *year;
@property (nonatomic,assign)NSUInteger page;

@end

@implementation WYSendOutController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.refresh = NO;
    // Do any additional setup after loading the view.
    [self setupUI];
    [self loadDataWithYear:@"2018"];
    self.year = @"2018";
    self.page = 1;
    WEAKSELF
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.refresh=NO;
        weakSelf.page++;
        // [weakSelf loadTableData];
        [weakSelf loadDataWithYear:self.year];
    }];
    
    
}
-(void)loadDataWithYear:(NSString *)year
{
    WEAKSELF
    [WYHttpServer getRedPacketMySendWithYear:year SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            weakSelf.receive = [WYMYReceive getMyReceiveModelFromJson:response[@"result"]];
            [weakSelf.tableView reloadData];
        }else{
            [weakSelf toast:errorMessage];
        }
        
    }];
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    [WYHttpServer getRedPacketMySendRecordWithYear:year WithCurrentPage:page SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            if (weakSelf.refresh) {
                [weakSelf.dataArray removeAllObjects];
            }
            
            [weakSelf.dataArray addObjectsFromArray:[WYSendRedPacketList getSendRedPacketListFromJson:response[@"result"][@"detail"]]];
            [weakSelf.tableView reloadData];
            
            if ([response[@"result"][@"hasNextPage"] integerValue]) {
                [weakSelf.tableView.mj_footer endRefreshing];
                
                
                
            }else{
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            
            
        }else{
            [weakSelf toast:errorMessage];
        }
        
        
    }];
    
    
    
    
    
}

-(void)setupUI
{
    [self.view addSubview:self.tableView];
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-88-44+20);
    [self.tableView registerNib:[UINib nibWithNibName:@"WYRedPacketListCell" bundle:nil] forCellReuseIdentifier:@"WYRedPacketListCell"];
    self.tableView.rowHeight = 72;
    [self.tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYRedPacketListCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WYRedPacketListCell" forIndexPath:indexPath];
    
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"WYRedPacketListCell" owner:nil options:nil].firstObject;
    }
    [cell setSelectionStyle:(UITableViewCellSelectionStyleNone)];
    WYSendRedPacketList *list = self.dataArray[indexPath.row];
    [cell setRedList:list];
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    WYRedSectionView *sectionView = [WYRedSectionView getSectionView];
    if (self.receive) {
        [sectionView setSReceive:self.receive];
    }
    sectionView.delegate = self;
     [sectionView.yearBtn setTitle:[NSString stringWithFormat:@"%@年",self.year] forState:(UIControlStateNormal)];
    return sectionView;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYSendRedPacketList *list =self.dataArray[indexPath.row];
    //    [[NSNotificationCenter defaultCenter] postNotificationName:@"seeProduct" object:@{@"service":service}];
    WYNearRedPacket *near = [[WYNearRedPacket alloc]init];
    near.redpacketId = list.redpacketId;
    near.redpacketType = @"2";
    near.isReceived = @"1";
    WYRedPacketMessageController *rvc = [[WYRedPacketMessageController alloc]init];
    rvc.redPacket = near;
    [self.navigationController pushViewController:rvc animated:YES];
    
    
    
    
}
-(void)didSelectYear
{
    WEAKSELF
    WYSelectYearView *yearView = [[WYSelectYearView alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth, kScreenHeight)];
    yearView.selectYear = self.year;
    yearView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    yearView.complte = ^(NSString *year) {
        weakSelf.year = year;
        [weakSelf.sectionView.yearBtn setTitle:[NSString stringWithFormat:@"%@年",year] forState:UIControlStateNormal];
        weakSelf.refresh = YES;
        [weakSelf loadDataWithYear:year];
        
        
    };
    
    
    [yearView show];
}




-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 224;
}
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStyleGrouped)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
    
    
}
@end
